package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.service.AdminActivityListService;
import org.linlinjava.litemall.core.cache.RemoteCacheService;
import org.linlinjava.litemall.core.storage.StorageService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;

@RestController
@RequestMapping("/admin/activity/front")
public class AdminActivityFrontController {
    private final Log logger = LogFactory.getLog(AdminActivityFrontController.class);

    @Autowired
    private AdminActivityListService adminActivityListService;

    @Autowired
    private StorageService storageService;

    @Autowired
    private RemoteCacheService remoteCacheService;

    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        return adminActivityListService.queryActivityDetail(id, true, null);
    }

    /**
     * @param activityId
     * @param type       1.浏览数 2.报名数 3.分享数 4.成团数
     * @return
     */
    @GetMapping("/statistics")
    public Object cacheStatistics(Integer activityId, Integer type) {
        return remoteCacheService.cacheStatistics(activityId, type);
    }

    @PostMapping("/pushMusic")
    public Object create(@RequestParam("file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        LitemallStorage litemallStorage = storageService.store(file.getInputStream(), file.getSize(),
                file.getContentType(), originalFilename);
        return ResponseUtil.ok(litemallStorage);
    }

}
